package edu.chl.grupp11.aegas.controller.imp;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JFileChooser;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.io.FileUtils;

import edu.chl.grupp11.aegas.constants.FileTypesConst;
import edu.chl.grupp11.aegas.controller.AbstractActionController;
import edu.chl.grupp11.aegas.controller.ErrorControllerFactory;
import edu.chl.grupp11.aegas.controller.FileControllerFactory;
import edu.chl.grupp11.aegas.controller.IBrowserController;
import edu.chl.grupp11.aegas.controller.IConfigController;
import edu.chl.grupp11.aegas.controller.IErrorController;
import edu.chl.grupp11.aegas.controller.IFileController;
import edu.chl.grupp11.aegas.controller.IPluginController;
import edu.chl.grupp11.aegas.controller.ITagController;
import edu.chl.grupp11.aegas.controller.TagControllerFactory;
import edu.chl.grupp11.aegas.model.AbstractConfig;
import edu.chl.grupp11.aegas.model.ConfigFactory;
import edu.chl.grupp11.aegas.view.AboutBox;
import edu.chl.grupp11.aegas.view.FileView;
import edu.chl.grupp11.aegas.view.HelpBox;
import edu.chl.grupp11.aegas.view.MainView;
import edu.chl.grupp11.aegas.view.SettingsBox;
import edu.chl.grupp11.aegas.view.TagView;

public class ActionController extends AbstractActionController {
	private IBrowserController browserController;
	private IFileController fileController;
	private MainView mainView;
	private ITagController tagController;
	private AbstractConfig config;
	private IErrorController errorController = ErrorControllerFactory
			.getErrorController();

	public ActionController(MainView mainView) {
		browserController = new BrowserController();
		fileController = FileControllerFactory.getFileController();
		tagController = TagControllerFactory.getTagController();
		this.mainView = mainView;
		config = ConfigFactory.getSharedConfig();
	}

	private void showAboutBox() {
		try {
			new AboutBox();
		} catch (Exception e) {
			showSwixmlError();
		}
	}

	private void showHelpBox() {
		try {
			new HelpBox();
		} catch (Exception e) {
			showSwixmlError();
		}
	}

	private void showSettingsBox() {
		SettingsBox sb;
		try {
			sb = new SettingsBox();
			IConfigController configController = new ConfigController();
			sb.addObserver(configController);
			sb.setText(config.getResourceBundle());
		} catch (Exception e) {
			showSwixmlError();
		}
	}

	private void showSwixmlError() {
		errorController.setErrorMessage(null, config.getResourceBundle()
				.getString("error15"));
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		errorController.setErrorMessage(null, "");
		if (arg1 != null) {
			ActionEvent e = (ActionEvent) arg1;
			if (e.getActionCommand().equals("OPEN_FILE")) {
				browserController.openFileBrowser();
			} else if (e.getActionCommand().equals("OPEN_FOLDER")) {
				browserController.openFolderBrowser();
			} else if (e.getActionCommand().equals("DELETE_ALL")) {
				fileController.deleteAllFiles();
			} else if (e.getActionCommand().equals("OPEN_URL")) {
				try {
					browserController.openURLBrowser();
				} catch (IOException e1) {
					errorController.setErrorMessage(e1, config
							.getResourceBundle().getString("error3"));
				}
			} else if (e.getActionCommand().equals("EXIT")) {
				MainView.shutDownApplication();
			} else if (e.getActionCommand().equals("SHOW_ABOUT_BOX")) {
				showAboutBox();

			} else if (e.getActionCommand().equals("SHOW_HELP_BOX")) {
				showHelpBox();

			} else if (e.getActionCommand().equals("SHOW_SETTINGS")) {
				showSettingsBox();

			} else if (e.getActionCommand().equals("SAVE")) {
				ArrayList<FileView> selectedFiles = fileController
						.getSelectedFiles();
				ArrayList<TagView> allTags = TagControllerFactory
						.getTagController().getVisibleTagViews();
				errorController.setErrorMessage(null, "");
				for (TagView tagView : allTags) {
					boolean hasChanged = tagView.hasChanged();
					if (!tagView.getValue().equals("[DIFFERENT VALUES]")
							&& hasChanged && tagView.hasValidUserInput()) {
						for (FileView fileView : selectedFiles) {
							tagController.editTag(fileView.getFile(),
									tagView.getIMetaTag(), tagView.getValue());
						}
					} else if (hasChanged && !tagView.hasValidUserInput()) {
						errorController.setErrorMessage(null, config
								.getResourceBundle().getString("error2"));
					}
				}
				setChanged();
				notifyObservers();

			} else if (e.getActionCommand().equals("CANCEL")) {
				for (FileView fv : fileController.getSelectedFiles()) {
					fv.setSelected(false);
					fileController.deselectFilesOfWrongType(fv);
				}
			} else if (e.getActionCommand().equals("ADD_PLUGIN")) {
				IPluginController pluginController = new PluginController();
				File plugin = pluginController.addPlugin();
				if (plugin != null) {
					pluginController.movePlugin(plugin);
					String bundleName = plugin.getName().substring(0,
							plugin.getName().lastIndexOf('.'));
					mainView.addLanguage(bundleName);
				}
			}

			else if (e.getActionCommand().equals("CHANGE_LANG")) {
				JRadioButtonMenuItem button = (JRadioButtonMenuItem) e
						.getSource();
				mainView.setLanguage(button.getText());

			}

			else if (e.getActionCommand().equals("INSTALL_EXTENSION")) {
				JFileChooser fc = new JFileChooser();
				FileNameExtensionFilter pluginFilter = FileTypesConst.PLUGIN_FILE_FILTER;
				fc.addChoosableFileFilter(pluginFilter);
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int resultat = fc.showOpenDialog(null);
				if (resultat == JFileChooser.APPROVE_OPTION) {
					try {
						FileUtils.copyFile(fc.getSelectedFile(), new File(
								"src/edu/chl/grupp11/aegas/controller/extensions/"
										+ fc.getSelectedFile().getName()));
					} catch (IOException e2) {
						errorController.setErrorMessage(e2, config
								.getResourceBundle().getString("error4"));
					}
				}
			}
			tagController.updateHandlers();
		}
	}
}
